/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.standardcloud.zuul.config;

import com.hyts.standardcloud.swagger.SwaggerBaseConfiguration;
import com.hyts.standardcloud.swagger.data.SwaggerApiDocRouteLocator;
import com.hyts.standardcloud.swagger.utils.SwaggerResourceUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @project-name:standard-cloud
 * @package-name:com.hyts.standardcloud.zuul.config
 * @author:LiBo/Alex
 * @create-date:2020-01-04 15:15
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
@Component
public class SwaggerRouteLocation implements SwaggerApiDocRouteLocator {

    @Autowired
    RouteLocator routeLocator;

    @Autowired
    DiscoveryClient discoveryClient;

    @Autowired
    ZuulProperties zuulProperties;

    @Value("${spring.application.version}")
    String version;


    @Value("${swagger.fetchByRegistry}")
    boolean fetchType;


    @Override
    public List<SwaggerResource> getServerRouterLocation() {
        //获取方式
        if(!fetchType){
           return zuulProperties.getRoutes().values().parallelStream().
                    map(param->SwaggerResourceUtils.swaggerResource(param.getServiceId(),"/"+param.getPath().
                                    replace(SwaggerBaseConfiguration.DEFAULT_REPLACE_PATH,
                                            SwaggerBaseConfiguration.DEFAULT_PREFX_PATH),
                            param.getUrl(),version)).collect(Collectors.toList());
        }else {
            /*discoveryClient.getServices().parallelStream().map(param->{
                discoveryClient.getInstances(param).parallelStream().forEach(param2->{

                 });
            }).collect(Collectors.toList());*/
            //注册中心服务操作
           /* discoveryClient.getServices().parallelStream().
                flatMap(param->Stream.of(discoveryClient.getInstances(param))).
                 collect(Collectors.toList()).forEach(param->param.forEach(param2->
                        SwaggerResourceUtils.swaggerResource(param2.getServiceId(),
                                "/"+param2.getServiceId()+SwaggerBaseConfiguration.DEFAULT_PREFX_PATH,
                                param2.getUri().getPath(),param2.getInstanceId())));*/
            return routeLocator.getRoutes().parallelStream().
                    map(param -> SwaggerResourceUtils.swaggerResource(param.getId(),
                            param.getFullPath().
                            replace(SwaggerBaseConfiguration.DEFAULT_REPLACE_PATH,
                                    SwaggerBaseConfiguration.DEFAULT_PREFX_PATH),
                                    param.getFullPath(),version)).collect(Collectors.toList());
        }
    }

}
